package com.xayy.ha.gen.lite.mapper;

import com.xayy.ha.gen.lite.entity.GenTabField;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClasssName CodeGenerateMapper
 * @Description 代码生成mapper
 * @Author Liuyh
 * @Date 2021/5/12
 * @Version V0.0.1
 */
@Mapper
@Component
public interface PgDbTableMapper {

    @Select("" +
            "SELECT   " +
            "  t4.relname as tableName ,  " +
            "  CAST ( obj_description ( t4.oid ) AS VARCHAR ) tableComment ,  " +
            "  t1.column_name columnName,  " +
            "  t1.udt_name dataType,  " +
            "  t3.description fieldComment,  " +
            "  t1.is_nullable isNullable,  " +
            "  COALESCE ( character_maximum_length, datetime_precision ) maxLength  " +
            "FROM  " +
            "  information_schema.COLUMNS t1  " +
            "  LEFT JOIN pg_attribute t2 ON (t1.column_name = t2.attname)  " +
            "  LEFT JOIN pg_description t3 ON (t3.objoid = t2.attrelid AND t3.objsubid = t2.attnum)  " +
            "  LEFT JOIN pg_class t4 ON (t2.attrelid = t4.oid)  " +
            "WHERE  " +
            "  table_schema = 'public'   " +
            "  AND TABLE_NAME = #{tableName}   " +
            "  AND t2.attnum >= 0   " +
            "  AND t4.relname =#{tableName} " +
            "")
    List<GenTabField> selectTableColumn(@Param("tableName") String tableName);
}